package com.xci.sys.controller;

import com.xci.core.annotation.Authorize;
import com.xci.core.base.RestMessage;
import com.xci.core.base.WebController;
import com.xci.core.exceptions.EntityNotFoundException;
import com.xci.core.web.JsonGrid;
import com.xci.sys.filter.HistoryLogFilter;
import com.xci.sys.service.HistoryLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * 系统历史记录控制器
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/sys/history")
public class HistoryController extends WebController {
    @Resource
    private HistoryLogService historyService;//历史记录服务

    //region 页面视图

    /**
     * 历史记录首页
     */
    @GetMapping
    public String index() {
        return "sys/history/index";
    }

    /**
     * 历史记录详细页面
     */
    @GetMapping("/details")
    public String details(String id, ModelMap map) {
        var entity = historyService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);
        map.put("entity", entity);
        return "sys/history/details";
    }

    //endregion

    //region 数据处理

    /**
     * 历史记录表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    public JsonGrid grid(HistoryLogFilter filter) {
        return RestMessage.grid(historyService.selectPageList(filter));
    }

    /**
     * 根据表名和主键查询历史记录分页列表
     */
    @ResponseBody
    @GetMapping("/gridbytable")
    public JsonGrid gridByTable(HistoryLogFilter filter){
        return RestMessage.grid(historyService.selectPageList(filter));
    }

    //endregion
}